<template>
 <Card :dis-hover="true">
    <base-table :data="data" :columns="columns" @add="addHandler" 
    @update="updateHandler" @check="checkHandler" @approve="approveHandler" @set="setHandler"
    @delete="deleteHandler" ck >
      <Select slot="select" v-model="currentSequence" style="width:200px" @on-change="selectChange">
        <Option v-for="item in sequences" :value="item.Id" :key="item.Id">{{ item.Name }}</Option>
      </Select>
    </base-table>
    <Modal   v-model="setProject" width="300"  title="设置项目级别" >
       
       <Form style="margin:5px;">
       <FormItem label="编号">
         <Input type="text" v-model="no"></Input>
       </FormItem>
       </Form>
       <div class="project_type"><Button  type="primary" ghost long @click="set(0)">设置为省级项目</Button></div>
       <div class="project_type"><Button  type="primary" ghost long @click="set(1)">设置为全国项目</Button></div>
       <div slot="footer">
            <!-- <Button type="error" size="large" long :loading="modal_loading" @click="del">Delete</Button> -->
        </div>
    </Modal>
  </Card>
</template>

<script>
import moment from 'moment';
import { SERVER_BASE_URL, STATIS_HOST, FDS_HOST } from '@/api/config';
import BaseTable from '@c/table/table';
import { newApiParams } from '@/common/utils';
export default {
  name: 'ProjectManager',
  components: {
    BaseTable
  },
  data () {
    return {
      currentSequence: '',
      sequences: [],
      setProject: false,
      setRow: {},
      data: [],
      no: '',
      columns: [
        { title: ' ', type: 'index', width: 50 },
        { title: '项目名称', key: 'Name', tooltip: true },
        { title: '项目编号', key: 'No' },
        {
          title: '项目类型',
          render: (h, { row }) => {
            let str = '';
            for (let k = 0; k < this.projectType.length; k++) {
              if (row['ProjectType'] === this.projectType[k].value) {
                str = this.projectType[k].key;
                break;
              }
            }
            return h('span', {}, str);
          }
        },
        { title: '项目进度', key: 'Type', tooltip: true },
        { title: '项目负责人', key: 'Creater' },

        // { title: '导师', key: 'TutorName' },
        { 
          title: '省级编号', 
          key: 'ProvinceNo', 
          render: (h, { row }) => { 
            return h('span', {}, row.ProvinceNo ? row.ProvinceNo : '暂无');
          } 
        },

        { 
          title: '预算经费',
          key: 'Budget',
          render: (h, { row }) => {
            let budget = row.Budget ? row.Budget : 0;
            
            return h('span', {}, `￥${budget}`);
          }
        },
        { 
          title: '已使用经费',
          key: 'Used',
          render: (h, { row }) => {
            let used = row.Used ? row.Used : 0;
            
            return h('span', {}, `￥${used}`);
          }
        },
        { title: '立项评分', key: 'Score1' },
        { title: '中期检查评分', key: 'Score2' },
        { title: '结项评分', key: 'Score3' },
        // {
        //   title: '项目级别',
        //   render: (h, { row }) => {
        //     let str = '';
        //     for (let k = 0; k < this.levelArray.length; k++) {
        //       if (row['Level'] === this.levelArray[k].value) {
        //         str = this.levelArray[k].key;
        //         break;
        //       }
        //     }
        //     return h('span', {}, str);
        //   }
        // },
        // {
        //   title: '项目来源',
        //   render: (h, { row }) => {
        //     let str = '';
        //     for (let k = 0; k < this.sourceArray.length; k++) {
        //       if (row['Source'] === this.sourceArray[k].value) {
        //         str = this.sourceArray[k].key;
        //         break;
        //       }
        //     }
        //     return h('span', {}, str);
        //   }
        // },
        // {
        //   title: '项目性质',
        //   render: (h, { row }) => {
        //     let str = '';
        //     for (let k = 0; k < this.chacterArray.length; k++) {
        //       if (row['Character'] === this.chacterArray[k].value) {
        //         str = this.chacterArray[k].key;
        //         break;
        //       }
        //     }
        //     return h('span', {}, str);
        //   }
        // },
        { 
          title: '截止时间',
          width: 200,
          render: (h, { row }) => {
            let start = moment(row['Start']).isValid() ? moment(row['Start']).format('YYYY/MM/DD') : 'YYYY/MM/DD';
            let end = moment(row['End']).isValid() ? moment(row['End']).format('YYYY/MM/DD') : 'YYYY/MM/DD';
            return h('span', {}, `${start} 至 ${end}`);
          }
        },
        {
          title: '申报书',
          // width: 150,
          align: 'center',
          render: (h, { row }) => {
            return h('a', {
              class: {
                download: true
              },
              attrs: {
                href: STATIS_HOST + row['WordUrl'],
                download: 'download',
                target: '_blank'
              },
              on: {
                click: (event) => {
                  event.preventDefault();
                  this.DownloadDocx(row.Id);
                }
              }
            }, '下载申报材料');
          }
        }
      ],
      levelArray: [
        {
          key: '全国',
          value: 0
        },
        {
          key: '省级',
          value: 1
        },
        {
          key: '市级',
          value: 2
        }
      ],
      sourceArray: [
        {
          key: '科研',
          value: 0
        },
        {
          key: '其它',
          value: 1
        }
      ],
      chacterArray: [
        {
          key: '软件工程',
          value: 0
        },
        {
          key: '其它',
          value: 1
        }
      ],
      projectType: [
        {
          key: '创新训练项目',
          value: 0
        },
        {
          key: '创业训练项目',
          value: 1
        },
        {
          key: '创业实践项目',
          value: 2
        }
        
      ]
    };
  },
  methods: {
    DownloadDocx (id) {
      // console.log(id);
      this.$api.DownloadDocx({ Id: id }).then(res => {
        if (res.code === 0) {
          console.log(res);
          window.open(FDS_HOST + res.Data);
        }
      });
    },
    selectChange () {
      this.getAll(this.currentSequence);
      for (let k = 0; k < this.sequences.length; k++) {
        // console.log(this.sequences[k]);
        if (this.sequences[k].Id === this.currentSequence) {
          this.currentSequenceName = this.sequences[k].Name;
          return;
        }
      }
    },
    GetSequences () {
      this.$api.GetSequences().then(res => {
        // console.log(res.Data);
        this.sequences = res.Data;
        if (Array.isArray(this.sequences) && this.sequences.length > 0) {
          this.currentSequence = this.sequences[0].Id;
          this.currentSequenceName = this.sequences[0].Name;
          this.getAll(this.sequences[0].Id);
        } else {
          this.$Message.warning('数据异常！');
        }
      }, err => {
        if (err) {
          console.debug(err);
        }
      });
    },
    addHandler () {},
    updateHandler () {},
    checkHandler (rows) {
      if (Array.isArray(rows) && rows.length === 1) {
        rows[0].Content = '';
        delete rows[0].Type;
        this.$router.push({ name: '项目审核', params: { project: rows[0], editorable: false, type: 1 } });
      } else {
        this.$Message.warning('请选择一条数据操作！');
      }
    },
    setHandler (rows) {
      console.log(rows);
      if (Array.isArray(rows) && rows.length === 1) {
        this.setRow = rows[0];
        this.setProject = true;
      } else {
        this.$Message.warning('请选择一条数据操作！');
      }
    },
    set (key) {
      this.setRow.ProvinceNo = this.no;
      if (key === 0) {
        this.setRow.Level = 1;
      } else {
        this.setRow.Level = 2;
      }
      this.$api.ImsProjectSetLevel({ Id: this.setRow.Id, Level: this.setRow.Level, ProvinceNo: this.setRow.ProvinceNo }).then((res) => {
        this.$Message.success('设置成功！');
        this.getAll();
        // // this.disableRepeatClick = false;
        // setTimeout(() => {
        //   this.$router.push({
        //     name: '项目申请'
        //   });
        // }, 500);
      }).catch((err) => {
        if (err) {
          this.$Message.error(err, 4);
          // this.disableRepeatClick = false;
        }
      });
    },
    approveHandler () {},
    deleteHandler () {},
    getAll () {
      this.$api.QueryLegeProject(this.currentSequence).then((res) => {
        // console.log(res.Data);
        if (Array.isArray(res.Data) && res.Data.length > 0) {
          this.data = res.Data;
        } else {
          this.data = [];
        }
      }).catch((err) => {
        if (err) {
          this.$Message.error(err);
        }
      });
    }
  },
  mounted () {
    this.GetSequences();
    // this.getAll();
  }
};
</script>

<style scoped>
.project_type {
  margin: 5px;
}
</style>